<?php
/**
 * Created by PhpStorm.
 * User: songs
 * Date: 2019/11/4
 * Time: 16:09
 */

namespace App\Services\Api;

use App\admin\Service\OA;
use App\Jobs\Fun;
use App\Models\Admin\OrderBaleModel;
use App\Models\Admin\OrderCheckModel;
use App\Models\Admin\OrderTempModel;
use App\Models\AreaModel;
use App\Models\CompanysModel;
use App\Models\LogModel;
use App\Models\Main\CompanyContractModel;
use App\Models\OrderCommentModel;
use App\Models\User\UserLevelModel;
use App\Models\User\UserModel;
use App\Services\Jiguang\JMessageApi;
use App\Services\OrdersPayService;
use App\Utilities\JiugeForm;
use Illuminate\Http\Request;
use App\Models\MoneyBilModel;
use DB;
use App\Models\User\UserOrdersModel;


///App\Services\Api\OrderTemp
class OrderTemp extends ApiBase
{


    /**
     * 获取工厂参与的货单列表
    */
    public function getOrderDocking(Request $request){
        $uid=OA::$user->getId();
        $orderTempArray = OrderTempModel::with('orderDetail')->where('fid',$uid)
            ->orderBy('id','DESC')
            ->get()->toArray();
        $list=[];
        foreach ($orderTempArray as $t){
            $status='未知';
            $zhongbiao=false;
            if($t['status']!= OrderTempModel::status_wancheng){
                $status=OrderTempModel::getstatus($t['status']);
            }else{
                $zhongbiao=true;
                $userOrder=\App\Models\User\UserOrdersModel::where([
                    'order_no'=>$t['out_trade_no']
                ])->select('status')->first();
                if($userOrder){
                    $status=\App\Models\User\UserOrdersModel::getStatus($userOrder['status']);
                }
            }
            $list[]=[
                'img'=>Fun::pImg($t['order_detail']['img']),
                'out_trade_no'=>$t['out_trade_no'],
                'bale'=>$t['bale'],
                'name'=>$t['order_detail']['name']??'',
                'com_name'=>CompanysModel::where('uid',$t['cid'])->value('com_name'),
                'period'=>$t['order_detail']['period'] ? date('Y-m-d ',$t['order_detail']['period']) : '',
                'status'=>$status,
                'zhongbiao'=>$zhongbiao,
                'uid'=>$t['cid'],
                'jiguang_id'=>JMessageApi::idToJiguang($t['cid']),
            ];
        }
        OA::Json(0,$list);
    }





    /**
     * 获取工厂参与的货单列表
     */
    public function orderXuanze(Request $request){
        $bale=$_POST['bale']??'';
        $baleModel= OrderBaleModel::where([
            'id'=>$bale,
            'uid'=> OA::$user->getId()
        ])->select('status')->first();
        if(!$baleModel){
            Fun::showmsg("订单错误");
        }
        $list= OrderTempModel::where([
            'bale'=>$bale,
            'cid'=> OA::$user->getId()
        ])
          //  ->where('status','!=',OrderTempModel::status_weizhifu)
            ->select([
                'bale','bond_money','cid'
                ,'fid','id'
                ,'notes','out_trade_no'
                ,'status','created_at'
            ])
            ->orderBy('id','DESC')
            ->limit(100)->get()
            ->toArray();
        foreach($list as $k=>$v){
            $list[$k]['jiguang_id']=JMessageApi::idToJiguang($v['fid']);
            $list[$k]['com_name']=CompanysModel::where('uid',$v['fid'])->value('com_name');
            $list[$k]['user']=UserModel::getinfo($v['fid']);
            $list[$k]['show_kongzhiqi']=$list[$k]['status']== OrderTempModel::status_wancheng;
            $list[$k]['status']=OrderTempModel::getstatus($list[$k]['status']);
            $list[$k]['created_at']=Fun::date($list[$k]['created_at'],false);
        }
        OA::Json(0,[
            'bale'=>[
                'id'=>$bale,
                ///'status'=>$baleModel->status,
                'select'=>$baleModel->status== OrderBaleModel::status_wancheng
            ],
            'list'=>$list
        ]);
    }


    public function getkongzhitai(){
        $out_trade_no = $_POST['out_trade_no'] ?? '';
        if ($out_trade_no == '') {
            OA::Json(1,'参数错误');
        }
        $orderTempModel = OrderTempModel::where(['out_trade_no' => $out_trade_no])->first();
        if (!$orderTempModel) {
            OA::Json(1,'订单不存在');
        }
        $out_trade_no=$orderTempModel->out_trade_no;
        $uid = OA::$user->getId();
        $type = null; ///gs表示公司  gc 表示工厂
        if ($orderTempModel->cid == $uid) {
            $type = 'gs';    ///表示公司
        } elseif ($orderTempModel->fid == $uid) {
            $type = 'gc';    ///表示工厂
        } else {
            OA::Json(1,'您无权限操作表单');
        }
        $OrderBaleModel = OrderBaleModel::where('id', $orderTempModel->bale)->first();
        if (!$OrderBaleModel) {
            OA::Json(1,'订单不存在');
        }
        $order = UserOrdersModel::
       /// with('orderFactory')
           /* ->with('orderDetail')
            ->with('orderCompany')*/
             where('order_no', $orderTempModel->out_trade_no)->first();
        if (!$order) {
            OA::Json(1,'订单不存在');
        }

        $yuanliao='';
        if($type=='gs'||$order['yuanliao_time']!=0){///如果是公司,则显示出原料,或者原料已经被设置为发送
            $yuanliao=$order['yuanliao'];
        }
        $show_fahuo=$type=='gc';///&&($order['status']<UserOrdersModel::status_daohuoqianshou);
        if($order['status']==UserOrdersModel::status_dabaofahuo){
            $show_fahuo=true;
        }
        if($order['status']==UserOrdersModel::status_daohuoqianshou){
            $show_fahuo=false;
        }


        OA::Json(0,[
            'name'=>$OrderBaleModel->name,
            'bale'=>$orderTempModel->bale,

            'gs_jiguang_id'=>JMessageApi::idToJiguang($order->cid),
            'gc_jiguang_id'=>JMessageApi::idToJiguang($order->fid),
            'out_trade_no'=>$orderTempModel->out_trade_no, //订单号
            'yuanliao'=>$yuanliao,                  ///原料清单
            'status'=>$order->status,               ///状态
            'type'=>$type,                          ///当前用户的身份类型

            'img'=>$OrderBaleModel->img,                ///货单的图片
            'gongsi'=>CompanysModel::where('uid', $orderTempModel['cid'])->value('com_name'),      ///发单公司
            'gongchang'=>CompanysModel::where('uid', $orderTempModel['fid'])->value('com_name'), ///接单工厂

            'qianshou'=>$order['status']>=UserOrdersModel::status_daohuoqianshou,////是否显示收货评价按钮
            'showwancheng'=>$type=='gs'&&$order['status']!=UserOrdersModel::status_wancheng,///是否显示订单完成按钮

            'com_name'=>$order['orderFactory']['com_name'] ?? '',///中单企业
            'com_address'=>$order['orderCompany']['com_address'] ?? '',///企业地址
            'legal_name'=>$order['orderFactory']['legal_name'] ?? '',///负责人
            ///
            'legal_mobile'=>$order['orderFactory']['legal_mobile'] ?? '',///联系方式
            'num'=>$order['num'] ?? 0,///订单数量
            'money'=>$order['money'] ?? 0,///订单金额
            ///
            'bond_money'=>$order['bond_money'] ?? 0,///保证金
            'allmoney'=>$order['num'] * $order['money'],///订单总价
            'note'=>$order['note'] ?? '',///厂家备注
            ///
            'created_at'=>$order['created_at'] ? date('Y-m-d H:i:s', $order['created_at']) : '',///下单时间
            ///
            'complete_time'=>Fun::date($OrderBaleModel->complete_time,false),
            'shengyutainshu'=>(int)(($OrderBaleModel->complete_time-time())/(60*60*24)),
            ///
            'kd_code'=>$order['kd_code'] ?? '',///快递运单
            'kd_name'=>$order['kd_name'] ?? '',///快递类型
            'shengchanjindu'=>UserOrdersModel::getStatus($order['status']),///生产进度

            'show_shouhuobtn'=>$type=='gs'&&$order['status']==UserOrdersModel::status_dabaofahuo,///是否显示收货按钮
            'show_qingdanbtn'=>$type=='gs'&&$order['yuanliao_time']==0,//是否显示 添加清单和发给工厂按钮
            'show_fahuo'=>$show_fahuo,
            /////是否显示发货表单
            'isfahuo'=>$order['kd_time']!=0,////是否发货
            'send_address'=>$order['send_address'] ?? '',///出发地
            'receiving_address'=>$order['receiving_address'] ?? '',///目的地
            'iswancheng'=>$order['complete']==UserOrdersModel::complete_yes, ////表示订单是完成
            'iscomment'=>OrderCommentModel::where([/////是否评价
                'out_trade_no'=>$orderTempModel->out_trade_no,
                'uid'=>$uid
            ])->exists()
        ]);
    }

}